// packageJ/outArea/outArea.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    tabList: [{
        name: '全部',
        status: ''
      },
      {
        name: '未结算',
        status: 0
      },
      {
        name: '已结算',
        status: 1
      },
    ],
    status: '',
    page: 1,
    isLoadMore: true,
    totalPage: 0,
    recordList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.name) {
      this.setData({
        name: options.name
      })
    }
    if (options.name != "phone_bill_recharge_dealer") {
      this.recordUser();
    }
    this.getRecordList();
  },
  ontabClick(event) {
    if (event.detail?.index === 0) {
      this.setData({
        status: '',
        page: 1,
        isLoadMore: true
      })
    } else {
      this.setData({
        status: event.detail?.name,
        page: 1,
        isLoadMore: true
      })
    }

    this.getRecordList();
  },
  recordUser() { //会员信息
    let urlStr = app.getNetAddresss("plugin.region-external-reward.frontend.controllers.record.bonus-record.get-top-data");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          this.setData({
            userInfo: data
          })
          if(data.income_name) {
            wx.setNavigationBarTitle({
              title: data.income_name,
            });
          }
          
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getRecordList() {
    let query = {};
    let urls = "plugin.region-external-reward.frontend.controllers.record.bonus-record.index";
    if (this.data.name == "phone_bill_recharge_dealer") {
      urls = "plugin.phone-bill-recharge.frontend.settlement.index";
      query = {
        page: this.data.page,
        status: this.data.status === 0 ? 'settled' : this.data.status == 1 ? 'unsettled' : ''
      }
    } else {
      query = {
        page: this.data.page,
        status: this.data.status,
      }
    }
    let urlStr = app.getNetAddresss(urls);
    app._postNetWork({
      url: urlStr,
      data: query,
      success: (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading(); //隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          if(this.data.name == "phone_bill_recharge_dealer"){
            this.setData({
              userInfo:data,
              recordList:data.settlements.data,
              total_page:data.settlements.last_page
            })
            if (!this.data.totalPage) {
              this.data.totalPage = 0;
            }
            wx.setNavigationBarTitle({
              title: data.plugin_name,
            })
            return
          }
          this.data.totalPage = data.bonus_record_list.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            recordList: data?.bonus_record_list?.data,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {};
      let urls = "plugin.region-external-reward.frontend.controllers.record.bonus-record.index";
      if (this.data.name == "phone_bill_recharge_dealer") {
        urls = "plugin.phone-bill-recharge.frontend.settlement.index";
        query = {
          page: this.data.page,
          status: this.data.status === 0 ? 'settled' : this.data.status == 1 ? 'unsettled' : ''
        }
      } else {
        query = {
          page: this.data.page,
          status: this.data.status,
        }
      }
      let urlStr = app.getNetAddresss(urls);
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            if(this.data.name == "phone_bill_recharge_dealer"){
              this.setData({
                recordList:this.data.recordList.concat(res.data.settlements.data)
              })
              return
            }
            let list = res?.data.bonus_record_list.data;
            this.data.isLoadMore = true;
            this.setData({
              recordList: this.data.recordList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      status: '',
      page: 1,
      isLoadMore: true,
    })
    this.getRecordList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline() {}
})